<template>
  <z-modal
      :width="modalWidth"
      :visible="visible"
      :title="title"
      destroyOnClose
      switchFullscreen
      @ok="handleSubmit"
      @cancel="close"
      cancelText="关闭"
  >
    <a-card :bordered="false">
      <!-- <a-form @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
		  <a-col :md="6" :sm="12">
		    <a-form-item label="部门名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
		      <a-input style="width:100%;" placeholder="请输入部门名称" v-model="queryParam.deptName" @search="searchQuery"></a-input>
		    </a-form-item>
		  </a-col>
          <a-col :span="4">
            <a-form-item>
              <a-button type="primary" icon="reload" style="margin-left: 8px" @click="searchReset()">重置</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form> -->
      <!--部门列表-->
      <vxe-grid
          id="selectDeptList"
          ref="vxeTable"
          :height="height"
          :toolbarConfig="false"
          :checkbox-config="{trigger:this.multi?'row':'cell'}"
          :radio-config="{trigger:!this.multi?'row':'cell'}"
          v-bind="gridOptions"
          v-on="listeners"
          :pager-config="false"
          :row-config="{isCurrent:true,keyField:'id',}"
          :treeConfig="{rowField: 'id',children: 'children',reserve:true}"
          @radio-change="onRadioSelectChange"
      >
      </vxe-grid>
    </a-card>
  </z-modal>
</template>

<script>
import VxeTableListMixin from '@/mixins/VxeTableListMixin'
import PageLayout from '@/layouts/PageLayout'
import { getAction } from '@/services/manage'
export default {
  name: 'SelectDeptModal',
  components: {
    PageLayout,
  },
  mixins: [VxeTableListMixin],
  props: {
    modalWidth: {
      type: Number,
      default: 1250,
      required: false
    },
    multi: {
      type: Boolean,
      default: false,
      required: false
    },
  },
  data() {
    return {
      title: '选择部门',
      loading: false,
      form: this.$form.createForm(this),
      visible: false,
      columns: [
        { type: this.multi ? 'checkbox' : 'radio', width: 50 },
        { type: 'seq', width: 40 },
        {
          title: '部门名称',
          field: 'deptName',
          treeNode: true,
          sortable: true
        },
        {
          title: '部门编码',
          field: 'deptCode',
          sortable: true
        },
        {
          title: '部门描述',
          field: 'deptDesc'
        },
        {
          title: '部门类型',
          field: 'deptType',
          formatter: 'dictText'
        },
      ],
      url: {
        list: "/dept/tree",
      },
      currentRow: {},
      dataSource: []
    }
  },
  methods: {
    async loadTableData() {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      var params = this.getQueryParams()// 查询条件
      this.loading = true
      const res = await getAction(this.url.list, params)
      // this.selectedRowKeys = []
      this.loading = false
      if (res.success) {
        this.dataSource = res.result
        this.setCurrentData()
      }
      return res
    },
    setCurrentData() {
      this.$refs.vxeTable.clearCurrentRow()
      let currentRow
      if (Object.keys(this.currentRow).length > 0) {
        this.dataSource.forEach(item => {
          if (this.currentRow.id === item.id) {
            currentRow = JSON.parse(JSON.stringify(item))
          }
        })
      }
      !currentRow ? currentRow = this.dataSource[0] || {} : ''
      this.currentRow = Object.assign({}, currentRow)
      this.$refs.vxeTable.setCurrentRow(currentRow)
    },
    showModal() {
      this.visible = true
      this.loadData()
      this.form.resetFields()
    },
    close() {
      this.searchReset(0)
      this.visible = false
    },
    handleSubmit() {
      this.$emit('ok', this.selectionRows, this.selectedRowKeys)
      this.close()
    },
    searchQuery() {
      this.loadData('reload')
    },
    onRadioSelectChange({ row }) {
      this.selectionRows = [row]
      this.selectedRowKeys = [row.num]
    }
  }
}
</script>
